package com.timmy.test1.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.ArrayMap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.timmy.test1.R;
import com.timmy.test1.common.CallBackMode;
import com.timmy.test1.common.Direction;
import com.timmy.test1.common.DirectionMode;
import com.timmy.test1.common.JKeyListener;
import com.timmy.test1.model.ViewPoint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JVKeyView extends RelativeLayout {
    public static boolean isCanTouchInVKey = true;

    public static float viewAlpha = 0.8F;

    private float flipX;

    private float flipY;

    private Map<Integer, Runnable> handlerMap = new HashMap<>();

    private boolean isMouseDown = false;

    private Map<Integer, View> longPressMap = (Map<Integer, View>) new ArrayMap();

    private Context mContext;

    private Handler mHandler = new Handler();

    public JKeyListener mJKeyListener;

    JVKeyMouseListener mJVKeyMouseListener;

    RockerViewListener mRockerViewListener;

    public List<RockerView> mRocks = new ArrayList<RockerView>();

    public List<RoundView> mVKeys = new ArrayList<RoundView>();

    private ViewGroup mView;

    private ArrayList<ViewPoint> mViewPoints = new ArrayList<ViewPoint>();

    private float mouseLastX;

    private float mouseLastY;

    private int mousePointCount;

    public JVKeyView(Context paramContext) {
        super(paramContext);
        this.mContext = paramContext;
    }

    public JVKeyView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        this.mContext = paramContext;
    }

    public JVKeyView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        this.mContext = paramContext;
    }

    private void changeKey(final int scanCode) {
        this.mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (RoundView roundView : JVKeyView.this.mVKeys) {
                    if (((scanCode == -40 && roundView.scanCode == -41) || (scanCode == -41 && roundView.scanCode == -40)) && roundView.isPressDown()) {
                        JVKeyView.this.doKeyUp((View) roundView, roundView.scanCode, (MotionEvent) null);
                    }
                }
            }
        }, 10L);
    }

    private void doKeyDown(final View v, final int scanCode, final MotionEvent event) {
        Runnable runnable;
        final KeyView keyView = (KeyView) v;
        if (keyView.isLongPress() && keyView.isPressDown()) {
            return;
        }

        if (keyView.isContinuous() && keyView.isPressDown()) {
            return;
        }

        if (v instanceof RoundView) {
            RoundView roundView = (RoundView) v;
            roundView.setBackground(roundView.mPressBackground);
        }
        if (keyView.isContinuous()) {
            runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        JVKeyView.this.postKeyDown(v, scanCode, event);
                        long l = (long) keyView.getContinuousFrequency() * 1000L - 20L;
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("连击按键---：");
                        stringBuilder.append(scanCode);
                        stringBuilder.append("----");
                        stringBuilder.append(l);
//                        Timber.i(stringBuilder.toString(), new Object[0]);

                        Log.e(TAG, stringBuilder.toString());
                        Thread.sleep(20L);
                        JVKeyView.this.mHandler.postDelayed(this, l);
                        return;
                    } catch (Exception exception) {
                        return;
                    }
                }
            };
            if (this.mHandler == null) {
                this.mHandler = new Handler();
            }
            this.mHandler.postDelayed(runnable, 100L);
            if (this.handlerMap == null) {
                this.handlerMap = new HashMap<>();
            }
            this.handlerMap.put(keyView.getScanCode(), runnable);
            return;
        }
        if (keyView.isLongPress()) {
            if (this.longPressMap == null) {
                this.longPressMap = new HashMap<>();
            }
            this.longPressMap.put(scanCode, keyView);
        }
        postKeyDown(v, scanCode, event);
    }

    private void doKeyUp(View paramView, int paramInt, MotionEvent paramMotionEvent) {
        KeyView keyView = (KeyView) paramView;
        if (keyView.isLongPress()) {
            if (!keyView.isPressDown()) {
                keyView.setPressDown(true);
                return;
            }
            keyView.setPressDown(false);
            Map<Integer, View> map = this.longPressMap;
            if (map != null && map.containsKey(Integer.valueOf(paramInt))) {
                this.longPressMap.remove(Integer.valueOf(paramInt));
            }
        }
        if (keyView.isContinuous()) {
            if (!keyView.isPressDown()) {
                keyView.setPressDown(true);
                return;
            }
            keyView.setPressDown(false);
            Handler handler = this.mHandler;
            if (handler != null) {
                handler.removeCallbacks(this.handlerMap.get(Integer.valueOf(keyView.getScanCode())));
            }
            Map<Integer, Runnable> map = this.handlerMap;
            if (map != null && map.containsKey(Integer.valueOf(paramInt))) {
                this.handlerMap.remove(Integer.valueOf(paramInt));
            }
        }
        if (paramView instanceof RoundView) {
            paramView.setBackground(((RoundView) paramView).mNormalBackground);
        }
        if (paramInt == -10) {
            this.mJVKeyMouseListener.onJVKeyLeftMouseUp();
            return;
        }
        if (paramInt == -11) {
            this.mJVKeyMouseListener.onJVKeyMiddleMouseUp();
            return;
        }
        if (paramInt == -20) {
            this.mJVKeyMouseListener.onJVKeyRightMouseUp();
            return;
        }
        if (paramInt == -21) {
            this.mJVKeyMouseListener.onJVKeyRightMouseUp();
            return;
        }
        if (paramInt == -30) {
            this.mJVKeyMouseListener.onJVKeyMouseScroll(1, paramMotionEvent);
            return;
        }
        if (paramInt == -31) {
            this.mJVKeyMouseListener.onJVKeyMouseScroll(-1, paramMotionEvent);
            return;
        }
        this.mJKeyListener.onJKeyUp(keyView);
    }

    private void findViews(ViewGroup paramViewGroup) {
        for (int i = 0; i < paramViewGroup.getChildCount(); i++) {
            if (paramViewGroup.getChildAt(i) instanceof ViewGroup) {
                findViews((ViewGroup) paramViewGroup.getChildAt(i));
            } else if (paramViewGroup.getChildAt(i) instanceof RoundView) {
                RoundView roundView = (RoundView) paramViewGroup.getChildAt(i);
                roundView.setAlpha(viewAlpha);
                this.mVKeys.add(roundView);
            } else if (paramViewGroup.getChildAt(i) instanceof RockerView) {
                final RockerView rockerView = (RockerView) paramViewGroup.getChildAt(i);
                rockerView.setAlpha(viewAlpha);
                final int type = rockerView.getRockerType();
                rockerView.setCallBackMode(CallBackMode.CALL_BACK_MODE_MOVE);

                rockerView.setOnShakeListener(DirectionMode.DIRECTION_8, new RockerView.OnShakeListener() {
                    @Override
                    public void direction(Direction param1Direction) {
                        if (mRockerViewListener != null) {
                            mRockerViewListener.onRockerViewDirection(param1Direction, type);
                        }
                    }

                    @Override
                    public void onFinish() {
                        if (type == 0) {
                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(R.mipmap.rock_wasd_normal)).getBitmap());
//                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(2131558857)).getBitmap());
                        } else {
                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(R.mipmap.rock_bg_out_normal)).getBitmap());

//                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(2131558855)).getBitmap());
                        }
                        if (JVKeyView.this.mRockerViewListener != null) {
                            JVKeyView.this.mRockerViewListener.onRockerViewFinish(type);
                        }
                    }

                    @Override
                    public void onStart() {
                        if (type == 0) {
                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(R.mipmap.rock_wasd_press)).getBitmap());
//                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(2131558858)).getBitmap());
                        } else {
                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(R.mipmap.rock_bg_out_press)).getBitmap());
//                            rockerView.setRockBackOut(((BitmapDrawable) JVKeyView.this.getResources().getDrawable(2131558856)).getBitmap());
                        }
                        if (JVKeyView.this.mRockerViewListener != null) {
                            JVKeyView.this.mRockerViewListener.onRockerViewStart(type);
                        }
                    }
                });

                rockerView.setOnAngleChangeListener(new RockerView.OnAngleChangeListener() {
                    @Override
                    public void angle(double param1Double) {
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("angele:");
                        stringBuilder.append(param1Double);
                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void onStart() {

                    }
                });
                this.mRocks.add(rockerView);
            }
        }
    }

    private static final String TAG = JVKeyView.class.getSimpleName();

    private void actionDown(MotionEvent motionEvent, int actionMasked, int actionIndex, int pointerId, float actionIndexX, float actionIndexY) {
        ViewPoint viewPoint;

        StringBuilder stringBuilder;
        Log.i(TAG, "ACTION_DOWN: 搜索坐标 actionIndexX: " + actionIndexX + " actionIndexY: " + actionIndexY);
        for (RockerView rockerView : mRocks) {
            if (touchEventInView(rockerView, actionIndexX, actionIndexY)) {
                stringBuilder = new StringBuilder();
                stringBuilder.append("ACTION_DOWN 找到的是 : Rock : ");
                stringBuilder.append(rockerView);
                Log.i(TAG, stringBuilder.toString());

                rockerView.doDwn(actionIndexX - rockerView.getX(), actionIndexY - rockerView.getY());
                viewPoint = new ViewPoint();
                viewPoint.downX = actionIndexX;
                viewPoint.downY = actionIndexY;
                viewPoint.pointId = pointerId;
                viewPoint.pointIndex = actionIndex;
                viewPoint.view = rockerView;
                this.mViewPoints.add(viewPoint);
                return;
            }
        }

        for (RoundView roundView : mVKeys) {
            if (touchEventInView(roundView, actionIndexX, actionIndexY)) {
                stringBuilder = new StringBuilder();
                stringBuilder.append("ACTION_DOWN 找到的是 : Key : ");
                stringBuilder.append(roundView.getText().toString());
                Log.e(TAG, stringBuilder.toString());

                doKeyDown(roundView, roundView.getScanCode(), motionEvent);

                if (roundView.isFling()) {
                    this.flipX = actionIndexX;
                    this.flipY = actionIndexY;
                }

                viewPoint = new ViewPoint();
                viewPoint.downX = actionIndexX;
                viewPoint.downY = actionIndexY;
                viewPoint.pointId = pointerId;
                viewPoint.pointIndex = actionIndex;
                viewPoint.view = roundView;
                this.mViewPoints.add(viewPoint);
                return;
            }
        }

        Log.e(TAG, "ACTION_DOWN: 鼠标");
        this.mousePointCount++;
        this.mouseLastX = actionIndexX;
        this.mouseLastY = actionIndexY;
        ViewPoint viewPoint2 = new ViewPoint();
        viewPoint2.downX = actionIndexX;
        viewPoint2.downY = actionIndexY;
        viewPoint2.pointId = pointerId;
        viewPoint2.pointIndex = actionIndex;
        viewPoint2.view = null;
        this.mViewPoints.add(viewPoint2);
        this.mJVKeyMouseListener.onDispatchTouchEvent(motionEvent);
        this.isMouseDown = true;
    }

    private void actionMove(MotionEvent motionEvent, int actionMasked, int actionIndex, int pointerId, float actionIndexX, float actionIndexY) {
        StringBuilder stringBuilder;
        for (int m = 0; m < motionEvent.getPointerCount(); m++) {
            stringBuilder = new StringBuilder();
            stringBuilder.append("pointerIndex=");
            stringBuilder.append(m);
            stringBuilder.append(", pointerId=");
            stringBuilder.append(motionEvent.getPointerId(m));
            Log.i(TAG, stringBuilder.toString());

            for (ViewPoint viewPoint1 : mViewPoints) {
                int pointerId1 = motionEvent.getPointerId(m);
                if (viewPoint1.pointId != -1 && pointerId1 == viewPoint1.pointId) {
                    actionIndexX = motionEvent.getX(motionEvent.findPointerIndex(viewPoint1.pointId));
                    actionIndexY = motionEvent.getY(motionEvent.findPointerIndex(viewPoint1.pointId));

                    if (viewPoint1.view == null) {
                        if (Math.abs(actionIndexX - viewPoint1.downX) > 10.0F || Math.abs(actionIndexY - viewPoint1.downY) > 10.0F) {
                            if (this.isMouseDown && this.mousePointCount == 1) {
                                this.mJVKeyMouseListener.onDispatchTouchEvent(motionEvent);
                                continue;
                            }

                            stringBuilder = new StringBuilder();
                            stringBuilder.append("ACTION_MOVE 鼠标 ");
                            stringBuilder.append(this.mouseLastX);
                            stringBuilder.append(" ");
                            stringBuilder.append(this.mouseLastY);
                            Log.d(TAG, stringBuilder.toString());

                            stringBuilder = new StringBuilder();
                            stringBuilder.append("ACTION_MOVE 鼠标 ");
                            stringBuilder.append(actionIndexX);
                            stringBuilder.append(" ");
                            stringBuilder.append(actionIndexY);
                            Log.d(TAG, stringBuilder.toString());

                            int moveX = (int) (actionIndexX - this.mouseLastX);
                            int moveY = (int) (actionIndexY - this.mouseLastY);
                            if (this.mousePointCount == 1) {
                                this.mJVKeyMouseListener.onJVKeyMove(moveX, moveY);
                                this.mouseLastX = actionIndexX;
                                this.mouseLastY = actionIndexY;
                            }
                        }
                        continue;
                    }

                    if (viewPoint1.view instanceof KeyView) {
                        KeyView keyView = (KeyView) viewPoint1.view;

                        if (Math.abs(actionIndexX - (viewPoint1.downX)) > 10.0F || Math.abs(actionIndexY - viewPoint1.downY) > 10.0F) {
                            stringBuilder = new StringBuilder();
                            stringBuilder.append("ACTION_MOVE Key ");
                            stringBuilder.append(keyView.getText());
                            stringBuilder.append(" ");
                            stringBuilder.append(actionIndexX);
                            stringBuilder.append(" ");
                            stringBuilder.append(actionIndexY);
                            Log.d(TAG, stringBuilder.toString());
                        }

                        if (keyView.isFling()) {
                            int moveX = (int) (actionIndexX - this.flipX);
                            int moveY = (int) (actionIndexY - this.flipY);
                            this.mJVKeyMouseListener.onJVKeyMove(moveX, moveY);
                            this.flipX = actionIndexX;
                            this.flipY = actionIndexY;
                        }
                        continue;
                    }

                    if (viewPoint1.view instanceof RockerView) {
                        RockerView rockerView = (RockerView) viewPoint1.view;
                        stringBuilder = new StringBuilder();
                        stringBuilder.append("ACTION_MOVE Rock ");
                        stringBuilder.append(viewPoint1.pointId);
                        stringBuilder.append(":");
                        stringBuilder.append(pointerId1);
                        Log.d(TAG, stringBuilder.toString());

                        stringBuilder = new StringBuilder();
                        stringBuilder.append("ACTION_MOVE Rock ");
                        stringBuilder.append(actionIndexX);
                        stringBuilder.append(" ");
                        stringBuilder.append(actionIndexY);
                        Log.d(TAG, stringBuilder.toString());
                        rockerView.doMove(actionIndexX - viewPoint1.view.getX(), actionIndexY - viewPoint1.view.getY());
                    }
                }
            }
        }
    }

    private void actionUp(MotionEvent motionEvent, int actionMasked, int actionIndex, int pointerId, float actionIndexX, float actionIndexY) {
        this.mousePointCount = 0;

        StringBuilder stringBuilder;

        for (ViewPoint viewPoint : mViewPoints) {
            if (pointerId == viewPoint.pointId) {
                if (viewPoint.view == null) {
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_UP 鼠标 ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e(TAG, stringBuilder.toString());
                    this.mouseLastX = actionIndexX;
                    this.mouseLastY = actionIndexY;
                    this.mJVKeyMouseListener.onDispatchTouchEvent(motionEvent);
                    continue;
                }

                if (viewPoint.view instanceof KeyView) {
                    KeyView keyView = (KeyView) viewPoint.view;
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_UP Key ");
                    stringBuilder.append(keyView.getText());
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e(TAG, stringBuilder.toString());

                    doKeyUp(keyView, keyView.getScanCode(), motionEvent);
                    continue;
                }
                if (viewPoint.view instanceof RockerView) {
                    RockerView rockerView = (RockerView) viewPoint.view;
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_UP Rock ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e(TAG, stringBuilder.toString());
                    rockerView.doUp();
                }
            }
        }

        this.isMouseDown = false;
        this.mViewPoints.clear();
    }

    private void actionCancel(MotionEvent motionEvent, int actionMasked, int actionIndex, int pointerId, float actionIndexX, float actionIndexY) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ACTION_CANCEL ");
        stringBuilder.append(actionIndexX);
        stringBuilder.append(" ");
        stringBuilder.append(actionIndexY);
        Log.e(TAG, stringBuilder.toString());

        for (ViewPoint viewPoint : mViewPoints) {
            if (viewPoint.getPointId() == pointerId) {
                if (viewPoint.view == null) {
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_CANCEL 鼠标 ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e("testEvent", stringBuilder.toString());
                    this.mouseLastX = actionIndexX;
                    this.mouseLastY = actionIndexY;
                    this.isMouseDown = false;
                    this.mJVKeyMouseListener.onDispatchTouchEvent(motionEvent);
                } else if (viewPoint.view instanceof KeyView) {
                    KeyView keyView = (KeyView) viewPoint.view;
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_CANCEL Key ");
                    stringBuilder.append(keyView.getText());
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e("testEvent", stringBuilder.toString());
                    doKeyUp(keyView, keyView.getScanCode(), motionEvent);
                } else if (viewPoint.view instanceof RockerView) {
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_CANCEL Rock ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e("testEvent", stringBuilder.toString());
                    RockerView rockerView = (RockerView) viewPoint.view;
                    rockerView.doUp();
                }
            }
        }
    }

    private void actionPointDown(MotionEvent motionEvent, int actionMasked, int actionIndex, int pointerId, float actionIndexX, float actionIndexY) {
        ViewPoint viewPoint;

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ACTION_POINTER_DOWN: 搜索坐标：");
        stringBuilder.append(actionIndexX);
        stringBuilder.append("  ");
        stringBuilder.append(actionIndexY);
        Log.e(TAG, stringBuilder.toString());

        for (RockerView rockerView : mRocks) {
            if (touchEventInView(rockerView, actionIndexX, actionIndexY)) {
                stringBuilder = new StringBuilder();
                stringBuilder.append("ACTION_POINTER_DOWN 找到的是 : Rock : ");
                stringBuilder.append(rockerView);
                Log.e(TAG, stringBuilder.toString());

                rockerView.doDwn(actionIndexX - rockerView.getX(), actionIndexY - rockerView.getY());
                viewPoint = new ViewPoint();
                viewPoint.downX = actionIndexX;
                viewPoint.downY = actionIndexY;
                viewPoint.pointIndex = actionIndex;
                viewPoint.pointId = pointerId;
                viewPoint.view = rockerView;
                this.mViewPoints.add(viewPoint);
                return;
            }
        }

        for (RoundView keyView : mVKeys) {
            if (touchEventInView(keyView, actionIndexX, actionIndexY)) {
                stringBuilder = new StringBuilder();
                stringBuilder.append("ACTION_POINTER_DOWN 找到的是 : Key : ");
                stringBuilder.append(keyView);
                Log.e(TAG, stringBuilder.toString());
                doKeyDown(keyView, keyView.getScanCode(), motionEvent);
                if (keyView.isFling()) {
                    this.flipX = actionIndexX;
                    this.flipY = actionIndexY;
                }

                viewPoint = new ViewPoint();
                viewPoint.downX = actionIndexX;
                viewPoint.downY = actionIndexY;
                viewPoint.pointId = pointerId;
                viewPoint.pointIndex = actionIndex;
                viewPoint.view = keyView;
                this.mViewPoints.add(viewPoint);
                this.isMouseDown = false;
                return;
            }
        }

        Log.e(TAG, "ACTION_POINTER_DOWN: 鼠标");
        this.mousePointCount++;
        if (this.isMouseDown) {
            this.mJVKeyMouseListener.onDispatchTouchEvent(motionEvent);
        }

        this.mouseLastX = actionIndexX;
        this.mouseLastY = actionIndexY;
        viewPoint = new ViewPoint();
        viewPoint.downX = actionIndexX;
        viewPoint.downY = actionIndexY;
        viewPoint.pointId = pointerId;
        viewPoint.pointIndex = actionIndex;
        viewPoint.view = null;
        this.mViewPoints.add(viewPoint);
    }

    private void actionPointerUp(MotionEvent motionEvent, int actionMasked, int actionIndex, int pointerId, float actionIndexX, float actionIndexY) {
        StringBuilder stringBuilder;
        for (ViewPoint viewPoint : mViewPoints) {
            if (pointerId == viewPoint.pointId) {
                if (viewPoint.view == null) {
                    int m = this.mousePointCount - 1;
                    this.mousePointCount = m;
                    if (m == 1) {
                        this.mJVKeyMouseListener.onDispatchTouchEvent(motionEvent);
                    }
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_POINTER_UP 鼠标 ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e(TAG, stringBuilder.toString());
                } else if (viewPoint.view instanceof KeyView) {
                    KeyView keyView = (KeyView) viewPoint.view;
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_POINTER_UP Key ");
                    stringBuilder.append(keyView.getText());
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e(TAG, stringBuilder.toString());
                    doKeyUp(keyView, keyView.getScanCode(), motionEvent);
                } else if (viewPoint.view instanceof RockerView) {
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("ACTION_POINTER_UP Rock ");
                    stringBuilder.append(actionIndexX);
                    stringBuilder.append(" ");
                    stringBuilder.append(actionIndexY);
                    Log.e(TAG, stringBuilder.toString());
                    ((RockerView) viewPoint.view).doUp();
                }
            }
        }
    }

    private boolean handlerEvent(MotionEvent paramMotionEvent) {
        int actionMasked = paramMotionEvent.getActionMasked();
        int actionIndex = paramMotionEvent.getActionIndex();
        int pointerId = paramMotionEvent.getPointerId(actionIndex);
        float actionIndexX = paramMotionEvent.getX(actionIndex);
        float actionIndexY = paramMotionEvent.getY(actionIndex);

        switch (actionMasked) {
            case MotionEvent.ACTION_DOWN:
                actionDown(paramMotionEvent, actionMasked, actionIndex, pointerId, actionIndexX, actionIndexY);
                break;
            case MotionEvent.ACTION_UP:
                actionUp(paramMotionEvent, actionMasked, actionIndex, pointerId, actionIndexX, actionIndexY);
                break;
            case MotionEvent.ACTION_MOVE:
                actionMove(paramMotionEvent, actionMasked, actionIndex, pointerId, actionIndexX, actionIndexY);
                break;
            case MotionEvent.ACTION_CANCEL:
                actionCancel(paramMotionEvent, actionMasked, actionIndex, pointerId, actionIndexX, actionIndexY);
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                actionPointDown(paramMotionEvent, actionMasked, actionIndex, pointerId, actionIndexX, actionIndexY);
                break;
            case MotionEvent.ACTION_POINTER_UP:
                actionPointerUp(paramMotionEvent, actionMasked, actionIndex, pointerId, actionIndexX, actionIndexY);
                break;
        }
        return true;
    }

    private void postKeyDown(View paramView, int paramInt, MotionEvent paramMotionEvent) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("scancode---postKeyDown:");
        stringBuilder.append(paramInt);
//        Timber.i(stringBuilder.toString(), new Object[0]);
        if (paramInt == -10) {
            this.mJVKeyMouseListener.onJVKeyLeftMouseDown();
        } else if (paramInt == -11) {
            this.mJVKeyMouseListener.onJVKeyMiddleMouseDown();
        } else if (paramInt == -20) {
            this.mJVKeyMouseListener.onJVKeyRightMouseDown();
        } else if (paramInt == -21) {
            this.mJVKeyMouseListener.onJVKeyRightMouseLongDown();
        } else if (paramInt == -30) {
            this.mJVKeyMouseListener.onJVKeyMouseScroll(1, paramMotionEvent);
        } else if (paramInt == -31) {
            this.mJVKeyMouseListener.onJVKeyMouseScroll(-1, paramMotionEvent);
        } else {
            this.mJKeyListener.onJKeyDown((KeyView) paramView);
        }
        if (paramInt == -40 || paramInt == -41) {
            changeKey(paramInt);
        }
//        if (JiActivityMenuHelper.isVibrate)
//            App.vibrator.vibrate(new long[]{0L, 100L}, -1);
    }

    private boolean touchEventInView(View paramView, float actionIndexX, float actionIndexY) {
        if (paramView == null) {
            return false;
        }
        int leftMargin = paramView.getLeft();
        int topMargin = paramView.getTop();
        int width = paramView.getMeasuredWidth();
        int height = paramView.getMeasuredHeight();
        return (actionIndexY >= topMargin && actionIndexY <= (height + topMargin) && actionIndexX >= leftMargin && actionIndexX <= (width + leftMargin));
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {
        return handlerEvent(paramMotionEvent);
    }

    public void releaseKey() {
        Handler handler = this.mHandler;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        if (this.handlerMap != null) {
            this.handlerMap = null;
        }
        Map<Integer, View> map = this.longPressMap;
        if (map != null) {
            for (Integer integer : map.keySet()) {
                JKeyListener jKeyListener = this.mJKeyListener;
                if (jKeyListener != null) {
                    jKeyListener.onJKeyUp((KeyView) this.longPressMap.get(integer));
                }
            }
        }
    }

    public void setContentView(ViewGroup paramViewGroup) {
        this.mView = paramViewGroup;
        this.mVKeys.clear();
        this.mRocks.clear();
        findViews(this.mView);
        addView((View) this.mView);
    }

    public void setJKeyListener(JKeyListener paramJKeyListener) {
        this.mJKeyListener = paramJKeyListener;
    }

    public void setJVKeyMouseListener(JVKeyMouseListener paramJVKeyMouseListener) {
        this.mJVKeyMouseListener = paramJVKeyMouseListener;
    }

    public void setRockerViewListener(RockerViewListener paramRockerViewListener) {
        this.mRockerViewListener = paramRockerViewListener;
    }

    public interface JVKeyMouseListener {
        void onDispatchTouchEvent(MotionEvent param1MotionEvent);

        void onJVKeyLeftMouseDown();

        void onJVKeyLeftMouseUp();

        void onJVKeyMiddleMouseDown();

        void onJVKeyMiddleMouseUp();

        void onJVKeyMouseScroll(int param1Int, MotionEvent param1MotionEvent);

        void onJVKeyMove(int param1Int1, int param1Int2);

        void onJVKeyRightMouseDown();

        void onJVKeyRightMouseLongDown();

        void onJVKeyRightMouseUp();
    }

    public interface RockerViewListener {
        void onRockerViewDirection(Direction param1Direction, int param1Int);

        void onRockerViewFinish(int param1Int);

        void onRockerViewStart(int param1Int);
    }
}